package mi.payment.ui.print;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.RemoteException;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.List;

import mi.payment.R;
import mi.payment.ui.print.helper.PrintController;
import mi.payment.transaction.AutoFinishActivity;
import mi.payment.utils.print.DataPrinterKernel;
import sunmi.sunmiui.title.Title;
import woyou.aidlservice.jiuiv5.ICallback;
import woyou.aidlservice.jiuiv5.IWoyouService;

/**
 * 打印交易明细界面
 *
 * @author bps .
 */
public class PrintTransactionDetailActivity extends AutoFinishActivity {

    private static final String EXTRA_LAST_BATCH = "extra_last_batch";

    private Title headTitle;
    private LinearLayout lin_print_detail;

    private PrintController printController;

    private boolean lastBatch;

    public static void start(Context context, boolean lastBatch) {
        Intent starter = new Intent(context, PrintTransactionDetailActivity.class);
        starter.putExtra(EXTRA_LAST_BATCH, lastBatch);
        context.startActivity(starter);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_print_transcation_detail);
        lastBatch = getIntent().getBooleanExtra(EXTRA_LAST_BATCH, false);

        lin_print_detail = (LinearLayout) this.findViewById(R.id.lin_print_detail);
        printController = new PrintController(this, lastBatch);
        printController.initPrintData();
        initView();
        fillData();
    }


    /**
     * 填充数据
     */
    private void fillData() {
        List<View> viewList = printController.getTransactionDetailView();
        for (int i = 0; i < viewList.size(); i++) {
            lin_print_detail.addView(viewList.get(i));
        }
        try {
            if (viewList.size() == 1) {
                String tag = (String) viewList.get(0).getTag();
                if ((!TextUtils.isEmpty(tag)) && tag.equals("NO_ORDER")) {
                    headTitle.setButtonVisibility(false, true, true, true);
                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    lp.setMargins(181, 160, 0, 0);
                    lin_print_detail.removeAllViews();
                    lin_print_detail.addView(viewList.get(0), lp);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initView() {
        headTitle = (Title) this.findViewById(R.id.title);
        headTitle.setTitleText(getString(R.string.transaction_detail_title));
        if (lastBatch) {
            headTitle.setTitleText(getString(R.string.print_last_batch_trans_detail));
        }
        headTitle.setHeadBgColor(Color.parseColor("#FD5A51"));
        headTitle.setImageViewAddDrawable(R.drawable.printer);
        headTitle.setButtonVisibility(false, false, true, true);
        headTitle.setMenuClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                print();
            }
        }, null, null);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            // 返回键
            case R.id.rel_back:
                finish();
                break;
            default:
                break;
        }
    }

    private void print() {
        IWoyouService printService = DataPrinterKernel.INSTANCE().getPrinterService();
        try {
            int printStatus = printService.updatePrinterState();
            if(printStatus == 1){
                headTitle.setButtonVisibility(false, true, true, true);
                printController.printTransactionDetail(mICallBack);
            }else{
                showToast(R.string.printer_error);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
            showToast(R.string.printer_error);
        }

    }

    private ICallback mICallBack = new BasePrintCallback() {

        @Override
        public void onPrintResult(int code, String msg) throws RemoteException {

        }

    };


}
